package com.amtf.datafile2.cfg.std;

import com.amtf.datafile2.DataField;
import com.amtf.datafile2.DataFieldDefinition;
import com.amtf.datafile2.DataFieldFormat;
import com.amtf.datafile2.DataFieldParser;
import com.amtf.datafile2.DataFieldType;
import com.amtf.datafile2.DataTableDefinition;
import com.amtf.datafile2.core.std.StdDataFieldFormat;
import com.amtf.datafile2.core.std.StdDataFieldParser;
import com.amtf.datafile2.core.std.StdDataFieldType;

public abstract class StdDataFieldDefinition<T extends DataTableDefinition<?>> extends StdDefinition implements DataFieldDefinition {

	protected T dataTableDefinition = null;
	protected boolean checkFormat = true;
	protected boolean allowEmpty = true;
	protected DataFieldType type = new StdDataFieldType("any");
	protected DataFieldParser parser = new StdDataFieldParser(this);
	protected DataFieldFormat format = new StdDataFieldFormat();
	// protected DataFieldFormat outFormat = new StdDataFieldFormat();
	// protected DataFieldFormat inFormat = new StdDataFieldFormat();
	protected boolean trimValue = false;
	protected String defaultValue = "";
	protected String description = "";

	public abstract DataFieldDefinition clone();

	public abstract DataField newDataFieldInstance();

	public StdDataFieldDefinition(String name) {
		super(name);
	}

	public StdDataFieldDefinition(String name, T dataTableDefinition) {
		this(name);
		this.dataTableDefinition = dataTableDefinition;
	}

	@Override
	public boolean equals(Object obj) {
		StdDataFieldDefinition<T> f1 = (StdDataFieldDefinition<T>) obj;

		return f1.name.equals(this.name) && f1.checkFormat == this.checkFormat && f1.type == this.type && f1.allowEmpty == this.allowEmpty &&
		// f1.format == this.format &&
				f1.trimValue == this.trimValue;
	}

	protected void copyTo(StdDataFieldDefinition<T> newObj) {
		newObj.dataTableDefinition = this.dataTableDefinition;
		newObj.name = this.name;
		newObj.checkFormat = this.checkFormat;
		newObj.allowEmpty = this.allowEmpty;
		newObj.type = this.type;
		newObj.parser = this.parser;
		// newObj.outFormat = this.outFormat;
		newObj.format = this.format;
		newObj.trimValue = this.trimValue;
	}

	// public DataField parse(String value) {
	// return parser.parse(value);
	// }
	//
	// public DataField parse(Object value) {
	// return parser.parse(value);
	// }
	//
	// public String format(DataField value) {
	// return format.format(value);
	// }

	public DataFieldFormat getFormat() {
		return format;
	}

	public void setFormat(DataFieldFormat format) {
		this.format = format;
	}

	// public DataFieldFormat getOutFormat() {
	// return outFormat;
	// }
	//
	// public void setOutFormat(DataFieldFormat format) {
	// this.outFormat = format;
	// }

	// public DataFieldFormat getInFormat() {
	// return inFormat;
	// }
	//
	// public void setInFormat(DataFieldFormat format) {
	// this.inFormat = format;
	// }

	public boolean isCheckFormat() {
		return checkFormat;
	}

	public void setCheckFormat(boolean checkFormat) {
		this.checkFormat = checkFormat;
	}

	public boolean isAllowEmpty() {
		return allowEmpty;
	}

	public void setAllowEmpty(boolean allowEmpty) {
		this.allowEmpty = allowEmpty;
	}

	public DataFieldType getType() {
		return type;
	}

	public void setType(DataFieldType type) {
		this.type = type;
	}

	public boolean isTrimValue() {
		return trimValue;
	}

	public void setTrimValue(boolean trimValue) {
		this.trimValue = trimValue;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public T getDataRecordDefinition() {
		return dataTableDefinition;
	}

	public void setDataRecordDefinition(T dataTableDefinition) {
		this.dataTableDefinition = dataTableDefinition;
	}

	public DataFieldParser getParser() {
		return parser;
	}

	public void setParser(DataFieldParser parser) {
		this.parser = parser;
	}
}
